package com.power.sifa.supervise.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.plf.core.base.model.Ha_attachment;
import com.plf.core.base.service.Ha_attachmentService;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.supervise.dao.Apl_feedbackdocDao;
import com.power.sifa.supervise.model.Apl_feedbackdoc;
import com.power.sifa.supervise.model.pageInfo.Apl_feedbackdocPageInfo;
import com.power.sifa.supervise.service.Apl_feedbackdocService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;
import java.util.function.BiFunction;

/**
 *  处理反馈材料业务层实现
 */
@Service
public class Apl_feedbackdocServiceImpl extends BaseServiceImpl implements Apl_feedbackdocService {

    @Autowired
    private Apl_feedbackdocDao dao;

    @Autowired
    private Ha_attachmentService ha_attachmentService;

    @Override
    public Apl_feedbackdoc getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Apl_feedbackdoc apl_feedbackdoc) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (apl_feedbackdoc.getPrimaryKey() != null) {
        	 Apl_feedbackdoc apl_feedbackdocfDb = dao.findById(apl_feedbackdoc.getPrimaryKey()).orElse(null);
             if(apl_feedbackdocfDb != null) {
                BeanUtils.copyProperties(apl_feedbackdoc, apl_feedbackdocfDb);
                dao.save(apl_feedbackdocfDb);
                return;
             }
        }
        dao.save(apl_feedbackdoc);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Apl_feedbackdoc> findAll(Apl_feedbackdocPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Apl_feedbackdoc> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Apl_feedbackdoc> list) {
		dao.saveAll(list);
	}

    @Override
    public List<Apl_feedbackdoc> queryFeedBackDocList(String applyid) {
        List<Apl_feedbackdoc> feedbackdocList = dao.queryFeedBackDocList(applyid);
        if (CollUtil.isNotEmpty(feedbackdocList)) {
            feedbackdocList.stream().forEach(feedbackdoc -> {
                String attachids = feedbackdoc.getAttachids();
                if (StringUtils.isNotEmpty(attachids)) {
                    List<String> attachIds = Arrays.asList(attachids.split(","));
                    queryFileUrlList(attachIds, feedbackdoc, (s1,s2) -> {
                        s1.setAttachmentList(s2);
                        return null;
                    });
                }
            });
            return feedbackdocList;
        }
        return Collections.emptyList();
    }


    private void queryFileUrlList(List<String> ids, Apl_feedbackdoc consumer, BiFunction<Apl_feedbackdoc, List<Ha_attachment>, Void> function) {
        if (CollUtil.isNotEmpty(ids)) {
            Map<String, Ha_attachment> ha_attachmentMap = ha_attachmentService.getHa_attachmentsByIds(ids);
            if (ObjectUtil.isNotNull(ha_attachmentMap)) {
                List<Ha_attachment> fileList = new ArrayList<>();
                ha_attachmentMap.forEach((k, v) -> fileList.add(v));
                function.apply(consumer, fileList);
            }
        }
    }
}
